package org.nch.videoLauncher.presenter;

import org.nch.videoLauncher.view.ISearchView;
import org.nch.videoLauncher.view.VideoDetailsView;
import org.nch.videoLauncher.task.VideoItemTask;
import org.nch.videoLauncher.domain.VideoItem;

import java.util.List;
import java.util.HashMap;

/**
 * Created by IntelliJ IDEA.
 * User: bill
 * Date: Jan 8, 2009
 * Time: 5:06:39 PM
 * To change this template use File | Settings | File Templates.
 */
public class SearchPresenter {

    private ISearchView view;
    private MainWindowPresenter mwPresenter;
    private VideoItemTask task;
    private List<VideoItem> resultsList;
    private VideoItem currentVideoItem;
    private VideoDetailsView videoDetailsView;

    public SearchPresenter(ISearchView view, MainWindowPresenter mwPresenter){
        this.view = view;
        this.mwPresenter = mwPresenter;
        task = new VideoItemTask();
    }

    public void quickSearch(){

        view.clearResultsList();
        resultsList = task.getQuickSearchResults(view.getQuickSearchText());
        showSearchResults();
    }

    public void movieSearch(){

        view.clearResultsList();
        HashMap searchTerms = new HashMap();

        if (!view.getDirectorText().equals(""))
            searchTerms.put("director", view.getDirectorText());
        if (!view.getCastText().equals(""))
            searchTerms.put("cast", view.getCastText());
        if (!view.getGenreText().equals(""))
            searchTerms.put("genre", view.getGenreText());

        resultsList = task.getMovieSearchResults(searchTerms);
        showSearchResults();
    }

    public void tvSearch(){

        view.clearResultsList();
        HashMap searchTerms = new HashMap();

        if (!view.getShowText().equals(""))
            searchTerms.put("show", view.getShowText());
        if (!view.getTvSeasonText().equals(""))
            searchTerms.put("season", Integer.parseInt(view.getTvSeasonText()));
        if (!view.getEpisodeText().equals(""))
            searchTerms.put("episode", Integer.parseInt(view.getEpisodeText()));

        resultsList = task.getTvSearchResults(searchTerms);
        showSearchResults();
    }

    public void sportsSearch(){

        view.clearResultsList();
        HashMap searchTerms = new HashMap();

        if (!view.getSportText().equals(""))
            searchTerms.put("sport", view.getSportText());
        if (!view.getSportSeasonText().equals(""))
            searchTerms.put("season", view.getSportSeasonText());
        if (!view.getTeamText().equals(""))
            searchTerms.put("team", view.getTeamText());

        resultsList = task.getSportsSearchResults(searchTerms);
        showSearchResults();
    }

    public void selectionChanged(){

        currentVideoItem = resultsList.get(view.getCurrentSelection());
        videoDetailsView = null;
        videoDetailsView = new VideoDetailsView();
        VideoDetailsPresenter detailsPresenter = new VideoDetailsPresenter(videoDetailsView, currentVideoItem);
        videoDetailsView.setPresenter(detailsPresenter);
        view.showDetailsView(videoDetailsView);

    }

    private void showSearchResults(){
        for (VideoItem vi : resultsList){
            view.addListElement(vi.getTitle());
        }
        view.showResultsTab();
        if (!resultsList.isEmpty())
            view.setCurrentSelection(0);
    }

}
